﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ScreenShark
{
    static class Screenshot
    {
        public static Image CaptureRectangle(Rectangle r)
        {
            var result = new Bitmap(r.Width, r.Height);

            using (var g = Graphics.FromImage(result))
            {
                g.CopyFromScreen(r.X, r.Y, 0, 0, r.Size);
            }

            return result;
        }


        public static Image CaptureActiveWindow()
        {
            WinApi.User32.RECT windowRect = new WinApi.User32.RECT();
            var hWnd = WinApi.User32.GetForegroundWindow();

            if (hWnd == IntPtr.Zero)
                return null;

            WinApi.User32.GetWindowRect(hWnd, ref windowRect);
            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;


            return CaptureRectangle(new Rectangle(windowRect.left, windowRect.top, width, height));
        }

    }
}
